package com.golxen.framework.activity;


import com.golxen.framework.event.condition.BaseActivityCondition;
import com.golxen.framework.activity.dao.entity.ActivityInfo;
import com.golxen.framework.event.condition.dao.entity.UserActivityConditionData;
import com.golxen.framework.user.player.GamePlayer;
import com.golxen.utils.TimeUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BaseActivityData {

    protected GamePlayer player;

    private ActivityInfo activityInfo;

    private List<BaseActivityCondition> baseActivityConditionList;

    private List<UserActivityConditionData> userActivityConditionDataList;

    public BaseActivityData(GamePlayer player, ActivityInfo activityInfo, List<BaseActivityCondition> baseActivityConditionList,
                            List<UserActivityConditionData> userActivityConditionDataList) {
        this.player = player;
        this.activityInfo = activityInfo;
        this.baseActivityConditionList = baseActivityConditionList;
        this.userActivityConditionDataList = userActivityConditionDataList;
    }

    public final void addToPlayer() {
        // 未完成状态
        for (BaseActivityCondition baseActivityCondition : baseActivityConditionList) {
            int conditionId = baseActivityCondition.getActivityConditionInfo().getConditionId();
            UserActivityConditionData userActivityConditionData = getUserActivityConditionData(conditionId);
            if (null != userActivityConditionData) {
                addTrigger(conditionId);
            }
        }
    }


    public void addTrigger(int conditionId) {
        for (BaseActivityCondition baseActivityCondition : baseActivityConditionList) {
            if (null != baseActivityCondition && baseActivityCondition.getConditionId() == conditionId) {
                baseActivityCondition.addTrigger(player);
            }
        }
    }

    private void removeTrigger(int conditionId) {
        for (BaseActivityCondition baseActivityCondition : baseActivityConditionList) {
            if (null != baseActivityCondition && baseActivityCondition.getConditionId() == conditionId) {
                baseActivityCondition.removeTrigger(player);
            }
        }
    }



    public final boolean canCompleted(int conditionId) {
        BaseActivityCondition baseActivityCondition = getBaseActivityCondition(conditionId);
        if(null != baseActivityCondition){
            return baseActivityCondition.isCompleted(player);
        }
        return false;
    }


    public GamePlayer getPlayer() {
        return player;
    }

    public List<BaseActivityCondition> getBaseActivityConditionList() {
        return baseActivityConditionList;
    }

    public List<UserActivityConditionData> getUserActivityConditionDataList() {
        return userActivityConditionDataList;
    }

    public boolean isContinueAddCondition(int type){

        for (BaseActivityCondition baseActivityCondition : baseActivityConditionList) {
            if(baseActivityCondition.getConditionType() == type){
                return false;
            }
        }
        return true;
    }

    public boolean isContainCondition(int conditionId) {
        if (null != userActivityConditionDataList) {
            for (UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
                if (userActivityConditionData.getConditionId() == conditionId) {
                    return true;
                }
            }
        }
        return false;
    }

    public UserActivityConditionData getFirstUserActivityConditionData() {
        return userActivityConditionDataList.get(0);
    }

    public UserActivityConditionData getFirstUserActivityConditionDataByType(int conditionType) {
        for(UserActivityConditionData userActivityConditionData : userActivityConditionDataList){
            if(userActivityConditionData.getType() == conditionType){
                return userActivityConditionData;
            }
        }
        return null;
    }

    public UserActivityConditionData getUserActivityConditionData(int conditionId) {
        for (UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
            if (userActivityConditionData.getConditionId() == conditionId) {
                return userActivityConditionData;
            }
        }
        return null;
    }

    //找出历史活动批次里积分最大值
    public Map<Integer, BigInteger> getMaxUserActivityConditionData() {
        Map<Integer, BigInteger> dataMap = new ConcurrentHashMap<>();
        if (null != userActivityConditionDataList) {
            for (UserActivityConditionData userActivityConditionData : userActivityConditionDataList) {
                if(!dataMap.containsKey(userActivityConditionData.getType())) {
                    dataMap.put(userActivityConditionData.getType(), BigInteger.ZERO);
                }
                BigInteger maxValue = dataMap.get(userActivityConditionData.getType());
                if (userActivityConditionData.getValue().compareTo(maxValue) > 0) {
                    dataMap.put(userActivityConditionData.getType(), userActivityConditionData.getValue());
                }
            }
        }
        return dataMap;
    }

    public BaseActivityCondition getBaseActivityCondition(int conditionId) {
        for (BaseActivityCondition baseActivityCondition : baseActivityConditionList) {
            if (baseActivityCondition.getConditionId() == conditionId) {
                return baseActivityCondition;
            }
        }
        return null;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public int getActivityId(){
        if(null != activityInfo){
            return activityInfo.getActivityId();
        }
        return 0;
    }

}
